<?php
/**
 * File to handle all API requests
 * Accepts GET and POST
 *
 * Each request will be identified by TAG
 * Response will be JSON data
 
  /**
 * check for POST request
 */
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
if (isset($_POST['tag'])) {
    // get tag
    $tag = $_POST['tag'];
 
    // include db handler
    //require_once 'include/DB_Functions.php';
    //$db = new DB_Functions();
 
    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);
 
    // check for tag type
    if ($tag == 'delete') {
	// To be implemented
        // Request type is Register new route
        $uid = $_POST['uid'];
        // check if route already exists
	$result = mysql_query("SELECT * from user_node WHERE user_id = '$uid'");
	$route = mysql_fetch_array($result);
 	$nid = $route["node_id"];
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            // user is already existed - error response
	    
	   
	    $delete1 = "DELETE FROM user_node WHERE user_id='$uid'";
	    $delete2 = "DELETE FROM node WHERE node_id='$nid'";
	    $res1=mysql_query($delete1) or die(mysql_error());
	    $res1=mysql_query($delete2) or die(mysql_error());
            $response["success"] = 1;
            $response["msg"] = "Route deleted";
            echo json_encode($response);
        } else {

		$response["error"] = 1;
                $response["error_msg"] = "No routes found";
                echo json_encode($response);
	}
    } else {
        echo "Invalid Request";
    }
} else {
    echo "Access Denied";
}
?>
